package ar.edu.caece.compiler.grammar.factories;

import ar.edu.caece.compiler.error.ParsingException;
import ar.edu.caece.compiler.grammar.AbstractTokenVisitorFactory;
import ar.edu.caece.compiler.grammar.Recognizer;
import ar.edu.caece.compiler.grammar.TokenRecognizer;
import ar.edu.caece.compiler.grammar.TokenVisitor;
import ar.edu.caece.compiler.grammar.attributes.Attribute;
import ar.edu.caece.compiler.grammar.attributes.AttributeSymbolTable;
import ar.edu.caece.compiler.grammar.attributes.AttributeUtils;
import ar.edu.caece.compiler.grammar.attributes.BooleanAttribute;
import ar.edu.caece.compiler.grammar.attributes.StringAttribute;
import ar.edu.caece.compiler.grammar.attributes.tree.AttributeTreeNode;
import ar.edu.caece.compiler.grammar.attributes.tree.NodeLlamadaFuncion;
import ar.edu.caece.compiler.grammar.attributes.tree.NodeVariable;
import ar.edu.caece.compiler.grammar.nonterminals.EXPR33;
import ar.edu.caece.compiler.grammar.nonterminals.POSIDE;
import ar.edu.caece.compiler.grammar.terminals.IdentifierToken;

public class EXPR33Factory extends AbstractTokenVisitorFactory {

	private static EXPR33Factory INSTANCE;
	
	private POSIDEFactory posideFactory;
	private TokenVisitor parsePOSIDE;
	
	public static EXPR33Factory getInstance() throws Exception {
		if(INSTANCE==null) INSTANCE = new EXPR33Factory();
		return INSTANCE;
	}
	
	private Initializer initializer = new Initializer() {
		
		@Override
		public void init() throws Exception {
			initializer = NoOperationInitializer.getInstance();
			posideFactory = POSIDEFactory.getInstance();
			parsePOSIDE = posideFactory.makeVisitor();
		}
	};
	
	private EXPR33Factory() throws Exception {

	}
	
	@Override
	public TokenVisitor makeChainedVisitor(
			final TokenVisitor siguiente) throws Exception {
		initializer.init();

		return new IdentifierToken.IdentifierChainVisitor(siguiente) {
			
			@Override
			public Recognizer identifierCase(IdentifierToken llamador) throws Exception {
				POSIDE poside = (POSIDE) nextToken().execute(parsePOSIDE);
				
				String tipo = "";
				
				String representation = llamador.getToken().getSymbol().getRepresentation();
				if(AttributeUtils.existe(representation,(AttributeSymbolTable) getAttribute(Attribute.TABLA_SIMBOLOS_HEREDADA))) {
					tipo = AttributeUtils.vartipo(representation, (AttributeSymbolTable) getAttribute(Attribute.TABLA_SIMBOLOS_HEREDADA));
				}
				
				poside.addAttribute(Attribute.LEXEMA, new StringAttribute(representation));
				EXPR33 expr33 = new EXPR33(llamador, poside);
				
				expr33.addAttribute("tipo",new StringAttribute(tipo));
				AttributeTreeNode tree = (AttributeTreeNode) poside.getAttribute(Attribute.ARBOL_SINTETIZADO);
				if(( (BooleanAttribute) poside.getAttribute("es")).getValue()) {
					NodeVariable var = (NodeVariable) tree;
					var.setIdentificador(representation);
				} else {
					if(!(tree instanceof NodeLlamadaFuncion)) {
						tree = new NodeVariable();
						((NodeVariable) tree).setIdentificador(representation);
					}
				}
				expr33.addAttribute(Attribute.ARBOL_SINTETIZADO, tree);
				return expr33;	
			}
		};
	
	}
	@Override
	public TokenVisitor makeVisitor() throws Exception {
		initializer.init();

		return new IdentifierToken.IdentifierTokenVisitor() {
			
			@Override
			public Recognizer defaultCase(TokenRecognizer llamador) throws Exception {
				throw new ParsingException("Error de parsing.",EXPR33.class,llamador);
			}
			
			@Override
			public Recognizer identifierCase(IdentifierToken llamador) throws Exception {
				POSIDE poside = (POSIDE) nextToken().execute(parsePOSIDE);
				
				String tipo = "";
				
				String representation = llamador.getToken().getSymbol().getRepresentation();
				if(AttributeUtils.existe(representation,(AttributeSymbolTable) getAttribute(Attribute.TABLA_SIMBOLOS_HEREDADA))) {
					tipo = AttributeUtils.vartipo(representation, (AttributeSymbolTable) getAttribute(Attribute.TABLA_SIMBOLOS_HEREDADA));
				}
				
				poside.addAttribute(Attribute.LEXEMA, new StringAttribute(representation));
				EXPR33 expr33 = new EXPR33(llamador, poside);
				
				expr33.addAttribute("tipo",new StringAttribute(tipo));
				
				
				AttributeTreeNode tree = (AttributeTreeNode) poside.getAttribute(Attribute.ARBOL_SINTETIZADO);
				if(( (BooleanAttribute) poside.getAttribute("es")).getValue()) {
					NodeVariable var = (NodeVariable) tree;
					var.setIdentificador(representation);
				} else {
					if(!(tree instanceof NodeLlamadaFuncion)) {
						tree = new NodeVariable();
						((NodeVariable) tree).setIdentificador(representation);
					}
				}
				expr33.addAttribute(Attribute.ARBOL_SINTETIZADO, tree);
				return expr33;
			}
		};
	}
	
	
}
